/**
 * 
 */
package com.vchello.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hxq
 * @time 2015年9月11日下午5:27:16
 */
public class DownFile {
		/**
		 * 下载文件
		 * @param file
		 */
		public static void downloadBp(File file,HttpServletRequest request,HttpServletResponse response) {
			// TODO Auto-generated method stub
			BufferedInputStream br;
			try {
				br = new BufferedInputStream(new FileInputStream(file));
				byte[] buf = new byte[1024];
				int len = 0;
				
				response.reset(); // 非常重要
	
				response.setContentType("application/xls;charset=UTF-8");
				response.setHeader("Content-Disposition",
						"attachment; filename="+new String(file.getName().getBytes("gb2312"),"ISO8859-1"));
				OutputStream out = response.getOutputStream();
				while ((len = br.read(buf)) > 0)
					out.write(buf, 0, len);
				br.close();
				out.flush();
				out.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}

